<?php

namespace common\observer;

use common\service\OriginalPlanNodeService;
use support\SingletonTrait;
use common\service\StatusService;
use teamones\dbeventObserver\interfaces\SplSubject;
use teamones\dbeventObserver\interfaces\SplObserver;

/**
 * 任务状态变化 通知执行人
 */
class OriginalPlanNodeDetailTaskClose implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        if (!isset($updateData['task_status'])) {
            return;
        }

        // 判断状态 == final  任务完成状态
        if ($updateData['task_status'] != StatusService::getInstance()->getTaskDoneStatus()['id']) {
            return;
        }

        try {
            $id = intval($subject->getSplPrimaryId());
            OriginalPlanNodeService::getInstance()->updateNodeDetailCloseType($id);
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
